// firstPackage/pages/findHouse/findHouse.js
import { submitAiFindHouse } from "../../../service/house_service"
import debounce from "../../../tools/debounce"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    showSinglePop: false,
    singlePopTitle: "",
    singlePopType: "",
    houseTargets: [
      {
        title: "刚需",
        value: "刚需",
        isChoose: false
      },
      {
        title: "结婚",
        value: "结婚",
        isChoose: false
      },
      {
        title: "养老",
        value: "养老",
        isChoose: false
      },
      {
        title: "改善",
        value: "改善",
        isChoose: false
      },
      {
        title: "教育",
        value: "教育",
        isChoose: false
      },
      {
        title: "投资",
        value: "投资",
        isChoose: false
      }
    ],
    houseArers: [
      {
        title: "50m²以下",
        value: "50m²以下",
        minArea: 0,
        maxArea: 50,
        isChoose: false
      },
      {
        title: "50-70m²",
        value: "50-70m²",
        minArea: 50,
        maxArea: 70,
        isChoose: false
      },
      {
        title: "70-90m²",
        value: "70-90m²",
        minArea: 70,
        maxArea: 90,
        isChoose: false
      },
      {
        title: "90-110m²",
        value: "90-110m²",
        minArea: 90,
        maxArea: 110,
        isChoose: false
      },
      {
        title: "110-130m²",
        value: "110-130m²",
        minArea: 110,
        maxArea: 130,
        isChoose: false
      },
      {
        title: "130-150m²",
        value: "130-150m²",
        minArea: 130,
        maxArea: 150,
        isChoose: false
      },
      {
        title: "130-150m²",
        value: "130-150m²",
        minArea: 130,
        maxArea: 150,
        isChoose: false
      },
      {
        title: "150-200m²",
        value: "150-200m²",
        minArea: 150,
        maxArea: 200,
        isChoose: false
      },
      {
        title: "200m²以上",
        value: "200m²以上",
        minArea: 200,
        maxArea: 999,
        isChoose: false
      }
    ],
    houseBedRooms: [
      {
        title: "一室",
        value: "一室",
        isChoose: false
      },
      {
        title: "二室",
        value: "二室",
        isChoose: false
      },
      {
        title: "三室",
        value: "三室",
        isChoose: false
      },
      {
        title: "四室",
        value: "四室",
        isChoose: false
      },
      {
        title: "五室",
        value: "五室",
        isChoose: false
      },
      {
        title: "五室+",
        value: "五室+",
        isChoose: false
      }
    ],
    //朝向
    directionArr: [
      {
        title: "朝东",
        value: "朝东",
        isChoose: false
      },
      {
        title: "朝南",
        value: "朝南",
        isChoose: false
      },
      {
        title: "朝西",
        value: "朝西",
        isChoose: false
      },
      {
        title: "朝北",
        value: "朝北",
        isChoose: false
      },
      {
        title: "南北通透",
        value: "南北通透",
        isChoose: false
      },
    ],
    //楼龄
    houseYears: [
      {
        title: "3年以内",
        value: "3年以内",
        isChoose: false
      },
      {
        title: "5年以内",
        value: "5年以内",
        isChoose: false
      },
      {
        title: "10年以内",
        value: "10年以内",
        isChoose: false
      },
      {
        title: "15年以内",
        value: "15年以内",
        isChoose: false
      },
      {
        title: "20年以内",
        value: "20年以内",
        isChoose: false
      },
      {
        title: "20年以上",
        value: "20年以上",
        isChoose: false
      },
    ],
    priceTitle: "首付预算多少",
    priceIndex: 0,


    formData: {
      "areaName": "",//区域
      "bedRoomNum": "",//几室
      "firstPay": "",//首付
      "maxArea": "",//面积
      "minArea": "",
      "totalPrice": "",
    },




  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },

  // 选择项选中切换
  selectModuleAction(e) {
    let type = e.currentTarget.dataset.type;
    let index = e.currentTarget.dataset.index;
    this.filterDealArr(type, index);
  },
  // 筛选处理:
  filterDealArr(type, index) {
    let arr = [];
    if (type == "target") {
      arr = this.data.houseTargets;
    } else if (type == "direction") {
      arr = this.data.directionArr;
    } else if (type == "year") {
      arr = this.data.houseYears;
    } else if (type == "bedRoomNum") {
      arr = this.data.houseBedRooms;
    } else if (type == "area") {
      arr = this.data.houseArers;
    }


    let isChoose = arr[index].isChoose;
    arr.forEach((item, idx) => {
      item.isChoose = false;
      if (idx == index) {
        item.isChoose = !isChoose
        if (type != "area") {
          let changeValue = `formData.${type}`;
          this.setData({
            [changeValue]: item.isChoose ? item.value : ""
          })
        } else {
          this.setData({
            'formData.minArea': item.isChoose ? item.minArea : 0,
            'formData.maxArea': item.isChoose ? item.maxArea : 0,
          })
        }

      }
    })

    //赋值:
    if (type == "target") {
      this.setData({
        houseTargets: arr
      })
    } else if (type == "direction") {
      this.setData({
        directionArr: arr
      })
    } else if (type == "year") {
      this.setData({
        houseYears: arr
      })
    } else if (type == "bedRoomNum") {
      this.setData({
        houseBedRooms: arr
      })
    } else if (type == "area") {
      this.setData({
        houseArers: arr
      })
    }
    console.log("JJLog:当前formData的值:" + JSON.stringify(this.data.formData));
  },


  //提交ai找房请求:
  submitAiFindHouse() {
    wx.showLoading();
    submitAiFindHouse(this.data.formData).then(res => {
      wx.hideLoading();
      console.log("JJLog:找房结果:" + JSON.stringify(res));
      if (res.code == 200) {

      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "找房失败",
        icon: "none"
      })
      console.error("JJLog:submitAiFindHouse", err);
    })
  },

  //出现单项选择弹窗:
  singleSelectAction(e) {
    let type = e.currentTarget.dataset.type;
    this.setData({
      showSinglePop: true,
      singlePopTitle: "请选择",
      singlePopType: type,
    })
  },

  //首付和总价切换
  mChangeItemAction(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      priceIndex: index
    })
    if (index == 0) {
      this.setData({
        'formData.totalPrice': ""
      })
    } else {
      this.setData({
        'formData.firstPay': ""
      })
    }
  },

  //单项选择回调:
  singlePopSelect(e) {
    console.log("JJLog:单项:" + JSON.stringify(e));
    let result = e.detail.result;
    let type = e.detail.type;
    let changeItem = `formData.${type}`
    this.setData({
      [changeItem]: result.value,
      showSinglePop: false
    })
  },

  singlePopHide() {
    this.setData({
      showSinglePop: false
    })
  },

  submitAction() {
    let data = this.data.formData;
    let isAllEmpty = false;
    for (let key in data) {
      console.log("JJLog:遍历值是:" + data[key]);
      if (data[key] != "") {
        isAllEmpty = true
      }
    }
    if (!isAllEmpty) {
      wx.showToast({
        title: '请至少选择一个找房条件',
        icon: "none"
      })
      return;
    }


    // if (!data.bedRoomNum) {
    //   wx.showToast({
    //     title: '请选择要找几室的房子',
    //     icon: "none"
    //   })
    //   return;
    // }
    // if (!data.maxArea) {
    //   wx.showToast({
    //     title: '请选择房屋面积',
    //     icon: "none"
    //   })
    //   return;
    // }
    console.log("JJLog:上传参数:" + JSON.stringify(this.data.formData));
    // debounce(this.submitAiFindHouse);
    app.globalData.aiData = this.data.formData;
    wx.navigateTo({
      url: "/firstPackage/pages/findHouseList/findHouseList",
    })
  },

})